<?php
/**
 * 登录注册处理
 * @author 老杨
 *
 */
class WeddingAction extends Controller {
	
	function WeddingAction() {
		parent::Controller ();
	}
	
	function login() {
		$this->load->library ('form_validation');
		$this->load->helper (array('form'));
		if (!$_POST) {
			$this->load->view ( 'wedding/login' );
		} else {
			//常规校验
			$this->form_validation->set_rules ('email', '电子邮件', 'trim|required|valid_email');
			$this->form_validation->set_rules ('password', '密码', 'required');
			if ($this->form_validation->run () == FALSE) {
			    $this->load->view ( 'wedding/login' );
				return;
			}
			//逻辑校验
			$this->form_validation->set_rules( 'login_error', '', 'callback__login_check');
			if ($this->form_validation->run () == FALSE) {
			    $this->load->view ( 'wedding/login' );
				return;
			}
			redirect("wedding/weddingManagerAction/index");
		}
	}
	
	function regist() {
		$this->load->helper ( array ('form', 'url' ) );
		$this->load->library ( 'form_validation' );
		//常规校验
		$this->form_validation->set_rules ('email', '邮箱', 'required|trim|valid_email|max_length[50]|xss_clean');
		$this->form_validation->set_rules ('name', '庆典公司名称', 'required|trim|max_length[30]|xss_clean');
		$this->form_validation->set_rules ('password', '密码', 'required|matches[repasswd]|xss_clean');
		$this->form_validation->set_rules ('repasswd', '密码确认', 'required');
		if ($this->form_validation->run () == FALSE) {
   			//初始化或出现错误的场合进入wedding/regist视图
			$this->load->view ('wedding/regist');
			return;
  		} 
  		//逻辑校验
		$this->form_validation->set_rules('regist_error', '', 'callback__regist_email_check');
		if ($this->form_validation->run() == FALSE) {
		    $this->load->view ( 'wedding/regist' );
			return;
		}
  		//登录成功后跳转到
  		$this->load->model ('wedding/weddingmodel', 'weddingModel', true);
  		$form_data = $this->_form_data ();
  		$form_data ['create_time'] = time ();
  		//未审核
  		$form_data ['flag'] = 0;
  		//email未验证
  		$form_data ['active_flag'] = 0;
  		$form_data ['password'] = md5 ($form_data ['password']);
  		unset ($form_data ['repasswd']);
  		$this->weddingModel->insert ($form_data);
  		//发送激活邮件
  		$this->load->library ('email'); 
		$this->email->from ('voyageme@163.com', '系统邮件');
		$this->email->to ($form_data['email']);
		$this->email->subject ('激活您在XX网的帐号');
		$this->email->message ('请激活您在XX网的帐号,测试邮件');
		$this->email->send();
		//转到页面
  		$this->load->view ('wedding/regist_success');
	}
	
	/**
	 * 注册逻辑校验
	 * @return string|string
	 */
	function _regist_email_check() {
		$this->load->model ('wedding/weddingmodel', 'weddingModel', true);
		$db_wedding = $this->weddingModel->get_wedding_by_email ($this->input->post ('email'));
		if ($db_wedding) {
			$this->form_validation->set_message ('_regist_email_check', '该email也被占用!');	
			return false;
		} else {
			return true;
		}
	}
	function _login_check() {
		$this->load->model('wedding/weddingmodel', 'weddingModel', true);
		$db_wedding = $this->weddingModel->get_wedding_by_email($this->input->post('email'));
		if ($db_wedding) {
			//email存在,验证密码的有效性
			if ($db_wedding['password'] != md5(trim($this->input->post('password')))) {
				$this->form_validation->set_message('_login_check', '密码错误!');	
				return false;			   
			} else {
				//验证成功,初始化cookie
				$this->load->library('session');
				$this->session->set_userdata('wedding_id',$db_wedding['id']);	
				return true;
			}
		} else {
			$this->form_validation->set_message('_login_check', '用户名不存在!');
			return false;
		}
	}
	
    function _form_data() {
		$form_data = array ();
		if ($_POST) {
			foreach ( $_POST as $k => $v ) {
				$form_data [$k] = $v;
			}
		}
		return $form_data;
	}
	function ajax_validate_email(){
		$this->load->model('wedding/weddingmodel', 'weddingModel', true);
		$db_wedding = $this->weddingModel->get_wedding_by_email($this->input->post('email'));
		if ($db_wedding) {
			//email存在
			echo "Email已经存在";
		} else {
			return;
		}
	}
	/*
	 * 处理提交的info信息,基本联系信息,包括电话头像,公司简介等
	 */
	function edit_info(){
		
	}
	
}